﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;

public partial class Add_Filter : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();
  
    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            getFilter();

            if (Request.QueryString["Id"] != null)
            {
                lblHeader.Text = "Edit Filter";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add Filter";
            }
        }

    }
    #endregion

    #region save cancel and clear
    protected void Save(object sender, EventArgs e)
    {
        clearMsg();
        FilterMaster filter = new FilterMaster();
        try
        {
            filter = new FilterMaster(_dataObject);
            BuildObject(filter);
            filter.GetFilterByName();

            if (Request.QueryString["Id"] != null)
            {
                if (filter.Id != Convert.ToInt32(Request.QueryString["Id"]) && filter.Id != 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> Filter already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(filter);
                filter.Id = Convert.ToInt32(Request.QueryString["Id"]);
                filter.UpdatedDate = System.DateTime.Now;
                filter.IsDeleted = false;
                filter.Update();
                divMessage.InnerHtml = "<strong>Success!</strong> Filter is updated successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
            else
            {
                if (filter.Id > 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> Filter already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(filter);
                filter.CreatedDate = System.DateTime.Now;
                filter.Create();

                divMessage.InnerHtml = "<strong>Success!</strong> Filter is added successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
                ClearControls();
            }
            getFilter();
        }
        catch (Exception ex)
        {
        }
    }
    protected void OnCancel(object sender,EventArgs e)
    {
        ClearControls();
        clearMsg();
    }
    private void ClearControls()
    {
        txtFilterName.Text = string.Empty;
    }
    #endregion
    #region binddata for add
    private void BindData()
    {
        FilterMaster filter = new FilterMaster();
        filter = new FilterMaster(_dataObject);
        filter.Id = Convert.ToInt32(Request.QueryString["Id"]);
        filter.GetFilterById();
        txtFilterName.Text = filter.Name; ;
    }
    private void BuildObject(FilterMaster filter)
    {
        filter.Name = txtFilterName.Text;
    }
    #endregion
    #region getdata and Itemcommand for listing
    private void getFilter()
    {
        FilterMaster filter = new FilterMaster();
        List<FilterMaster> filters = new List<FilterMaster>();
        filter = new FilterMaster(_dataObject);

        filters = filter.GetAllFilter();

        if (filters != null)
        {
            rptFilter.DataSource = filters;
            rptFilter.DataBind();
        }
    }
    protected void ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        clearMsg();
        if (e.CommandName == "Delete")
        {
            FilterMaster filter = new FilterMaster();
            filter = new FilterMaster(_dataObject);
            filter.Id = Convert.ToInt32(e.CommandArgument);
            filter.Delete();
            getFilter();
           
        }
        if (e.CommandName == "Edit")
        {
            divMessage.InnerHtml = "";
            divMessage.Attributes.Add("class", "");
            ((TextBox)e.Item.FindControl("txtName")).Visible = true;
            ((Label)e.Item.FindControl("lblName")).Visible = false;

            ((LinkButton)e.Item.FindControl("lnkEdit")).Visible = false;
            ((LinkButton)e.Item.FindControl("lnkDelete")).Visible = false;
            ((LinkButton)e.Item.FindControl("lnkUpdate")).Visible = true;
            ((LinkButton)e.Item.FindControl("lnkCancel")).Visible = true;
            ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "TableAdvanced.init();", true);
        }
        if (e.CommandName == "cancel")
        {

            ((TextBox)e.Item.FindControl("txtName")).Visible = false;
            ((Label)e.Item.FindControl("lblName")).Visible = true;

            ((LinkButton)e.Item.FindControl("lnkEdit")).Visible = true;
            ((LinkButton)e.Item.FindControl("lnkDelete")).Visible = true;
            ((LinkButton)e.Item.FindControl("lnkUpdate")).Visible = false;
            ((LinkButton)e.Item.FindControl("lnkCancel")).Visible = false;
          
        }
        if (e.CommandName == "update")
        {
            FilterMaster filter = new FilterMaster();
            filter = new FilterMaster(_dataObject);
            filter.Name = ((TextBox)e.Item.FindControl("txtName")).Text;
            filter.Id=Convert.ToInt32(e.CommandArgument);
            filter.GetFilterByName();
            if (filter.Id != Convert.ToInt32(e.CommandArgument) && filter.Id != 0)
            {
                divListmsg.InnerHtml = "<strong>Error!</strong> Filter already Exists.";
                divListmsg.Attributes.Add("class", "alert alert-danger");
                return;
            }
            
            filter.UpdatedDate = System.DateTime.Now;
            filter.IsDeleted = false;
            filter.Update();
            getFilter();
          
            divListmsg.InnerHtml = "<strong>Success!</strong> Filter is updated successfully.";
            divListmsg.Attributes.Add("class", "alert alert-success");
        }
    }
    private void clearMsg()
    {
        divListmsg.InnerHtml = "";
        divListmsg.Attributes.Add("class", "");
        divMessage.InnerHtml = "";
        divMessage.Attributes.Add("class", "");
    }
    #endregion
}